/**
 * 六合彩的逻辑
 */
initGame(game, function() {
    fillTable();
    fillHeXiao();
    fillBuZhong();
    fillLianXiao();
    fill_lianma();
});

function fillTable() {
    // 特码49个号码
    var table = $(".temaa");
    table.empty();
    for (var i = 1; i <= 10; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 40; j += 10) {
            var k = j < 10 ? "0" + j : j;
            if (j > 49) {
                tr.append("<td> </td>");
                continue;
            }
            add_bet_item('特码' + k, draw_number(j), tr);
        }
        table.append(tr);
    }

    var table = $(".temab");
    table.empty();
    for (var i = 1; i <= 10; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 40; j += 10) {
            var k = j < 10 ? "0" + j : j;
            if (j > 49) {
                tr.append("<td> </td>");
                continue;
            }
            add_bet_item('特码B' + k, draw_number(j), tr);
        }
        table.append(tr);
    }

    // 大小单双(特码页面)
    var arr = ['大', '单', '合大', '合单', '小', '双', '合小', '合双', '尾大', '大单', '小单', '尾小', '大双', '小双'];
    var table = $(".dxds");
    table.empty();
    var f = function(start, count, table) {
        var tr = $("<tr></tr>");
        for (var i = start; i < start + count; i++) {
            add_bet_item('特' + arr[i], '特' + arr[i], tr);
        }
        if (count < 4) tr.append("<td> </td>");
        table.append(tr);
    }
    f(0, 4, table); f(4, 4, table);
    f(8, 3, table); f(11, 3, table);

    // 两面盘
    var arr = ['大', '小', '单', '双', 
        '合大', '合小', '合单', '合双', 
        '天肖', '地肖', '前肖', '后肖', '家肖', '野肖', 
        '大尾', '小尾', 
        '大单', '小单', '大双', '小双'];
    var table = $(".lmp");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in arr) {
        var k = arr[i];
        if (k[1] == '肖') k = k[0];
        if (k == '大尾') k = '尾大';
        if (k == '小尾') k = '尾小';
        add_bet_item('特' + k, '特' + arr[i], tr);
        if (i % 4 == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    var tr = $("<tr></tr>");
    add_bet_item('和大', '总和大', tr);
    add_bet_item('和小', '总和小', tr);
    add_bet_item('和单', '总和单', tr);
    add_bet_item('和双', '总和双', tr);
    table.append(tr);

    // 色波
    var table = $(".sebo tbody");
    table.empty;
    var f = function(numbers, k, name, table) {
        var tr = $("<tr></tr>");
        var td = $("<td></td>");
        for (var i in numbers) {
            td.append(draw_number(numbers[i]));
        }
        tr.append(td);

        add_bet_item(k, name, tr);
        table.append(tr);
    };
    f(gameRule.hongbo(), '特红', '红波', table);
    f(gameRule.lanbo(), '特蓝', '蓝波', table);
    f(gameRule.lvbo(), '特绿', '绿波', table);

    // 半波
    var arr = ['大', '小', '单', '双'];
    var arr2 = ['红', '蓝', '绿'];
    var table = $(".banbo tbody");
    table.empty();
    for (var i in arr2) {
        var tr = $("<tr></tr>");
        for (var j in arr) {
            var k = '特' + arr2[i] + arr[j];
            add_bet_item(k, arr2[i] + arr[j], tr);
        }
        table.append(tr);
    }

    // 半半波
    var arr = ['大单', '大双', '小单', '小双'];
    var table = $(".banbanbo tbody");
    table.empty();
    for (var i in arr2) {
        var tr = $("<tr></tr>");
        for (var j in arr) {
            var k = '特' + arr2[i] + arr[j];
            add_bet_item(k, arr2[i] + arr[j], tr);
        }
        table.append(tr);
    }

    // 特肖
    var table = $(".texiao");
    table.empty();
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    var tr = $("<tr></tr>");
    for (var i in arr) {
        add_bet_item('特' + arr[i], arr[i], tr);
        if (i % 4 == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }

    // 特码头尾数
    var table = $(".tematws")
    table.empty();
    for (var i = 0; i <= 4; i++) {
        var tr = $("<tr></tr>");
        add_bet_item('特头' + i, i + '头', tr);
        add_bet_item('特尾' + (i + 1), (i + 1) + '尾', tr);
        if (i + 6 < 10)
            add_bet_item('特尾' + (i + 6), (i + 6) + '尾', tr);
        else
            add_bet_item('特尾0', '0尾', tr);
        table.append(tr);
    }

    // 正码
    var table = $(".zhengma")
    table.empty();
    for (var i = 1; i <= 10; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 40; j += 10) {
            if (j > 49) {
                tr.append("<td> </td>");
                continue;
            }
            add_bet_item("正" + j, draw_number(j), tr);
        }
        table.append(tr);
    }

    // 正一特~正六特
    var arr = ['一', '二', '三', '四', '五', '六'];
    var tables = [];
    for (var i = 1; i <= 6; i++) tables.push($(".zt" + i));
    for (var z in arr) {
        var table = tables[z];
        table.empty();
        for (var i = 1; i <= 10; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 40; j += 10) {
                var k = j < 10 ? "0" + j : j;
                if (j > 49) {
                    tr.append("<td> </td>");
                    continue;
                }
                add_bet_item(arr[z] + '码' + k, draw_number(j), tr);
            }
            table.append(tr);
        }
    }
    var arr2 = ['大', '单', '合单', '小', '双', '合双', '红', '蓝', '绿'];
    var tables = [];
    for (var i = 1; i <= 6; i++) tables.push($(".ztdxds" + i));
    var tr = $("<tr></tr>");
    for (i in arr) {
        var table = tables[i];
        table.empty();
        for (var j in arr2) {
            add_bet_item(arr[i] + arr2[j], '正' + (parseInt(i) + 1) + '-' + arr2[j], tr);
            if (j % 3 == 2) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
    }

    // 正码1~6
    var arr = ['一', '二', '三', '四', '五', '六'];
    var arr2 = ['单', '双', '大', '小', '合单', '合双', '合大', '合小', '红', '蓝', '绿', '尾大', '尾小'];
    var div = $(".zhengma16");
    div.empty();
    for (var z in arr) {
        var table = $('<table class="container"></table>');
        table.append('<tr><th>正码' + arr[z] + '</th></tr>');
        for (var i in arr2) {
            var tr = $("<tr></tr>");
            var name = arr2[i];
            if (i < 4) name += '码';
            if (name == '红' || name == '蓝' || name == '绿') name += '波';
            add_bet_item(arr[z] + arr2[i], name, tr);
            table.append(tr);
        }

        var li = $("<li></li>");
        li.append(table);
        div.append(li);
    }

    // 五行
    var table = $(".wuxing tbody");
    table.empty;
    f(gameRule.jin(), '特金', '金', table);
    f(gameRule.mu(), '特木', '木', table);
    f(gameRule.shui(), '特水', '水', table);
    f(gameRule.huo(), '特火', '火', table);
    f(gameRule.tu(), '特土', '土', table);

    // 一肖
    var table = $(".yixiao tbody");
    table.empty;
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    var tr = $("<tr></tr>");
    for (var i in arr) {
        add_bet_item('平特' + arr[i], arr[i], tr);
        if (i % 4 == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }

    // 尾数
    var table = $(".weishu tbody");
    table.empty;
    var tr = $("<tr></tr>");
    for (var i = 0; i <= 9; i++) {
        add_bet_item('平特尾' + i, "平特" + i + '尾', tr);
        if (i % 4 == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    tr.append("<td></td>"); tr.append("<td></td>");
    table.append(tr);

    // 正肖
    var table = $(".zhengxiao");
    table.empty;
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    var tr = $("<tr></tr>");
    for (var i in arr) {
        add_bet_item('正' + arr[i], arr[i], tr);
        if (i % 4 == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }

    // 七色波
    var table = $(".qisebo tbody");
    table.empty;
    var tr = $("<tr></tr>");
    add_bet_item('七红', '红波', tr); tr.append("<td class='line'><div class='line'></div></td>");
    add_bet_item('七蓝', '蓝波', tr); tr.append("<td class='line'><div class='line'></div></td>");
    add_bet_item('七绿', '绿波', tr); tr.append("<td class='line'><div class='line'></div></td>");
    add_bet_item('七和', '和局', tr);
    table.append(tr);

    // 总肖
    var table = $(".zongxiao tbody");
    table.empty;
    var tr = $("<tr></tr>");
    for (var i = 2; i <= 7; i++) {
        add_bet_item('总' + i, i + '肖', tr);
        if (i == 4) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    table.append(tr);
    var tr = $("<tr></tr>");
    add_bet_item('总单', '总肖单', tr);
    add_bet_item('总双', '总肖双', tr);
    tr.append("<td></td>");
    table.append(tr);
}

function draw_number(n) {
    n = parseInt(n);
    var k = n < 10 ? '0' + n : n;
    var hong = gameRule.hongbo(), lan = gameRule.lanbo(), lv = gameRule.lvbo();
    var c = '';
    if (hong.indexOf(n) != -1) c = 'hongbo';
    else if (lan.indexOf(n) != -1) c = 'lanbo';
    else if (lv.indexOf(n) != -1) c = 'lvbo';
    return '<div class="' + c + '">' + k + '</div>';
}

/************ 合肖的处理start */
function fillHeXiao() {
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    var table = $(".hexiao_numbers thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < 6; i++) {
        tr.append("<th>生肖</th>");
        tr.append("<th>勾选</th>");
    }
    table.append(tr);

    table = $(".hexiao_numbers tbody");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in arr) {
        tr.append("<td class='img'>" + arr[i] + "</td>");
        tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + arr[i] + '" data-am-ucheck></label></td>');
        if (i == 5) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    table.append(tr);

    $(".hexiao_numbers input").change(function(e) {
        refresh_hexiao_numbers();
    });
}

function refresh_hexiao_numbers() {
    var arr = $(".hexiao_numbers input");
    var list = get_hexiao_numbers();
    var max = 11;

    arr.uCheck('enable');
    if (list.length >= max) {
        for (var i = 0; i < arr.length; i++) {
            if (!arr[i].checked)
                $(arr[i]).uCheck('disable');
        }
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }

    // 设置赔率
    var n = list.length;
    if (n < 2) {
        $(".hexiao-title .rate").html("--");
    }
    else {
        var k = get_hexiao_target();
        var rate = gameRule.map[k] ? gameRule.map[k].rate : "--";
        $(".hexiao-title .rate").html(rate);
    }
}

function get_hexiao_target() {
    var list = get_hexiao_numbers();
    if (list.length < 2) return null;
    return '合' + list.length;
}

function get_hexiao_numbers() {
    var arr = $(".hexiao_numbers input");
    var list = [];
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

function clear_hexiao() {
    $(".hexiao_numbers input").uCheck('enable');
    $(".hexiao_numbers input").uCheck('uncheck');
}

function hexiao_reset() {
    clear_hexiao();
}

function hexiao_do_bet(v) {
    var list = get_hexiao_numbers();
    if (list.length < 2) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var key = get_hexiao_target();
    key = key + ":" + list.join(",");
    var data = {};
    data[key] = v * 100;
    show_bet_confirm_list(data);
}
/************ 合肖的处理end */

/************ 自选不中的处理start */
function fillBuZhong() {
    var table = $(".buzhong_numbers thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < 5; i++) {
        tr.append("<th>号</th>");
        tr.append("<th>勾选</th>");
    }
    table.append(tr);

    table = $(".buzhong_numbers tbody");
    table.empty();
    for (var i = 1; i <= 10; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 40; j += 10) {
            if (j > 49) {
                tr.append("<td> </td>");
                tr.append("<td> </td>");
                continue;
            }
            tr.append("<td class='img'>" + draw_number(j) + "</td>");    
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + j + '" data-am-ucheck></label></td>');
        }
        table.append(tr);
    }

    $(".buzhong_numbers input").change(function(e) {
        refresh_buzhong_numbers();
    });
}

function refresh_buzhong_numbers() {
    var arr = $(".buzhong_numbers input");
    var list = get_buzhong_numbers();
    var max = 12;

    arr.uCheck('enable');
    if (list.length >= max) {
        for (var i = 0; i < arr.length; i++) {
            if (!arr[i].checked)
                $(arr[i]).uCheck('disable');
        }
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }

    // 设置赔率
    var n = list.length;
    if (n < 5) {
        $(".buzhong-title .rate").html("--");
    }
    else {
        var k = get_buzhong_target();
        var rate = gameRule.map[k] ? gameRule.map[k].rate : "--";
        $(".buzhong-title .rate").html(rate);
    }
}

function get_buzhong_target() {
    var list = get_buzhong_numbers();
    if (list.length < 5) return null;
    return '不中' + list.length;
}

function get_buzhong_numbers() {
    var arr = $(".buzhong_numbers input");
    var list = [];
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

function clear_buzhong() {
    $(".buzhong_numbers input").uCheck('enable');
    $(".buzhong_numbers input").uCheck('uncheck');
}

function buzhong_reset() {
    clear_buzhong();
}

function buzhong_do_bet(v) {
    var list = get_buzhong_numbers();
    if (list.length < 5) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var key = get_buzhong_target();
    key = key + ":" + list.join(",");
    var data = {};
    data[key] = v * 100;
    show_bet_confirm_list(data);
}
/************ 自选不中的处理end */


/********** 连肖连尾处理start */
function fillLianXiao() {
    var types = ['连肖2', '连肖3', "连肖4", "连肖5", "连尾2", "连尾3", "连尾4", "连尾5"];
    var table = $(".lianxiao thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + types[i] + '" data-am-ucheck></label></th>');
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<td>' + gameRule.getTargetName(types[i]) + "</td>");
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        var key = types[i];
        var rate = gameRule.map[key] ? gameRule.map[key].rate : "--";
        tr.append('<td class="special-rate" key="' + key + '">' + rate + "</td>");
    }
    table.append(tr);
    $($(".lianxiao input")[0]).uCheck("check");
    $(".lianxiao input").change(function(e) {
        fill_lianxiao_nubmers();
    });
    fill_lianxiao_nubmers();
}

function fill_lianxiao_nubmers() {
    var key = get_lianxiao_target();
    if (key.indexOf('连肖') != -1) {
        // 绘制生肖选择
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        var table = $(".lianxiao_numbers thead");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < 4; i++) {
            tr.append("<th>生肖</th>");
            tr.append("<th>赔率</th>");
            tr.append("<th>勾选</th>");
        }
        table.append(tr);

        table = $(".lianxiao_numbers tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i in arr) {
            var k = key + arr[i];
            var rate = gameRule.map[k] ? gameRule.map[k].rate : "--";
            tr.append("<td class='img'>" + arr[i] + "</td>");
            tr.append('<td class="special-rate" key="' + k + '">' + rate + "</td>");
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + arr[i] + '" data-am-ucheck></label></td>');

            if (i % 4 == 3) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
    }
    else {
        // 绘制尾数选择
        var table = $(".lianxiao_numbers thead");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < 4; i++) {
            tr.append("<th>种类</th>");
            tr.append("<th>赔率</th>");
            tr.append("<th>勾选</th>");
        }
        table.append(tr);

        table = $(".lianxiao_numbers tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < 12; i++) {
            if (i > 9) {
                tr.append("<td colspan='3'> </td>");
                continue;
            }
            var k = key + i;
            var rate = gameRule.map[k] ? gameRule.map[k].rate : "--";
            tr.append("<td class='img'>" + i + "尾</td>");
            tr.append('<td class="special-rate" key="' + k + '">' + rate + "</td>");
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + i + '" data-am-ucheck></label></td>');

            if (i % 4 == 3) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        table.append(tr);
    }
    
    $(".lianxiao_numbers input").change(function(e) {
        refresh_lianxiao_numbers();
    });
}

// 当前的投注类型
function get_lianxiao_target() {
    return $(".lianxiao input:checked").val();
}

// 取得当前选中的数字列表
function get_lianxiao_numbers() {
    var list = [];
    var arr = $(".lianxiao_numbers input");
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

// 清理选中的数字
function clear_lianxiao() {
    $(".lianxiao_numbers input").uCheck('enable');
    $(".lianxiao_numbers input").uCheck('uncheck');
}

// 刷新连肖的数字信息
function refresh_lianxiao_numbers() {
    var arr = $(".lianxiao_numbers input");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }
}

// 确定投注的处理
function lianxiao_do_bet(v) {
    var key = get_lianxiao_target();
    var limit = parseInt(key[2]);
    var list = get_lianxiao_numbers();
    if (limit > list.length) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var arr;
    if (limit == 2) arr = group2(list);
    else if (limit == 3) arr = group3(list);
    else if (limit == 4) arr = group4(list);
    else if (limit == 5) arr = group5(list);
    var data = {};
    for (var i in arr) {
        data[key + ":" + arr[i].join(",")] = v * 100;
    }

    console.log(data);
    show_bet_confirm_list(data);
}
function lianxiao_reset() {
    clear_lianxiao();
}

/********** 连肖连尾处理end */

/*************处理连码 */
// 填充连码
function fill_lianma() {
    var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
    var table = $(".lianma thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in types) {
        if (i == 0)
            tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + types[i] + '" data-am-ucheck checked></label></th>');
        else
            tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + types[i] + '" data-am-ucheck></label></th>');
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<td>' + gameRule.getTargetName(types[i]) + "</td>");
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        var key = types[i];
        var rate = gameRule.map[key] ? gameRule.map[key].rate : "--";
        if (types[i] == '中3') {
            // 2个赔率
            var k2 = '三中三';
            var rate2 = gameRule.map[k2] ? gameRule.map[k2].rate : "--";
            var line = '<td class="two"><div class="special-rate" key="中3">__r1__</div><div class="special-rate" key="三中三">__r2__</div></td>';
            line = line.replace(/__r1__/, rate);
            line = line.replace(/__r2__/, rate2);
            tr.append(line);
        }
        else if (types[i] == '中2') {
            // 2个赔率
            var k2 = '二中特';
            var rate2 = gameRule.map[k2] ? gameRule.map[k2].rate : "--";
            var line = '<td class="two"><div class="special-rate" key="中2">__r1__</div><div class="special-rate" key="二中特">__r2__</div></td>';
            line = line.replace(/__r1__/, rate);
            line = line.replace(/__r2__/, rate2);
            tr.append(line);
        }
        else
            tr.append('<td class="special-rate" key="' + key + '">' + rate + "</td>");
    }
    table.append(tr);
    $($(".lianma input")[0]).uCheck("check");

    // 填充49个号码
    table = $(".numbers thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < 5; i++) {
        tr.append("<th>号</th>");
        tr.append("<th>勾选</th>");
    }
    table.append(tr);

    table = $(".numbers tbody");
    table.empty();
    for (var i = 1; i <= 10; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 40; j += 10) {
            if (j > 49) {
                tr.append("<td colspan='2'> </td>");
                continue;
            }
            tr.append("<td class='img'>" + draw_number(j) + "</td>");
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + j + '" data-am-ucheck></label></td>');
        }
        table.append(tr);
    }

    register_lianma_event();
}

// 连码的事件处理
function register_lianma_event() {
    $(".lianma input").change(function(e) {
        clear_lianma();
    });
    $(".numbers input").change(function(e) {
        refresh_lianma_numbers();
    });
}

// 当前的投注类型
function get_lianma_target() {
    var i = $(".lianma input:checked").val();
    if (i == '') {
        $($(".lianma input")[0]).uCheck("check");
        i = '中3';
    }
    return i;
}

// 取得当前选中的数字列表
function get_lianma_numbers() {
    var arr = $(".numbers input");
    var list = [];
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

// 清理选中的数字
function clear_lianma() {
    $(".numbers input").uCheck('enable');
    $(".numbers input").uCheck('uncheck');
}

// 刷新连码的数字信息
function refresh_lianma_numbers() {
    var arr = $(".numbers input");
    var list = get_lianma_numbers();
    var k = get_lianma_target();
    var max = 7;
    if (k == '全3') max = 10;
    if (k == '全4') max = 4;

    arr.uCheck('enable');
    if (list.length >= max) {
        // 其他数字需要置灰
        for (var i = 0; i < arr.length; i++) {
            if (!arr[i].checked)
                $(arr[i]).uCheck('disable');
        }
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }
}

// 确定投注的处理
function lianma_do_bet(v) {
    var k = get_lianma_target();
    var list = get_lianma_numbers();
    var limit = 2;
    if (k != '特串') limit = parseInt(k[1]);
    if (v < 1 || list.length < limit) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var key = get_lianma_target();
    key = key + ":" + list.join(",");
    var data = {};
    data[key] = v * 100;
    show_bet_confirm_list(data);
}
function lianma_reset() {
    clear_lianma();
}

window.parse_target_name = function(k) {
    var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
    var arr = k.split(":");
    if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
        var limit = 2;
        if (arr[0] != '特串') limit = parseInt(arr[0][1]);
        var list = arr[1].split(",");

        // 组数
        var betList = [];
        if (limit == 2) betList = group2(list);
        else if (limit == 3) betList = group3(list);
        else if (limit == 4) betList = group4(list);
        return gameRule.getTargetName(arr[0]) + " " + arr[1] + "[" + betList.length + "组]"
    }
    return gameRule.getTargetName(k);
}
window.calc_bet_list = function(data) {
    var total = 0, count = 0;
    var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
    for (var k in data) {
        var arr = k.split(":");
        if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
            var list = arr[1].split(',');
            var limit = 2;
            if (arr[0] != '特串') limit = parseInt(arr[0][1]);

            // 组数
            var betList = [];
            if (limit == 2) betList = group2(list);
            else if (limit == 3) betList = group3(list);
            else if (limit == 4) betList = group4(list);
            count += betList.length;
            total += data[k] * betList.length;
            continue;
        }
        count++;
        total += data[k];
    }
    return [count, total];
}
window.unpack_bet_list = function(data) {
    var r = {};
    var types = ['中3', '全3', '全2', '中2', '特串', '全4'];
    for (var k in data) {
        var arr = k.split(":");
        if (arr.length > 1 && types.indexOf(arr[0]) != -1) {
            var list = arr[1].split(',');
            var limit = 2;
            if (arr[0] != '特串') limit = parseInt(arr[0][1]);

            // 组数
            var betList = [];
            if (limit == 2) betList = group2(list);
            else if (limit == 3) betList = group3(list);
            else if (limit == 4) betList = group4(list);
            for (var i in betList) {
                r[arr[0] + ':' + betList[i].join(",")] = data[k];
            }
            continue;
        }
        else
            r[k] = data[k];
    }
    return r;
}

/********************** 处理连码结算 */

// 自定义的开奖结果绘制
window.customDrawResult = function(i) {
    i = parseInt(i);
    return draw_number(i) + "<div class='xiao'>" + gameRule.getShengXiao(i) + "</div>";
}